﻿using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNet.Identity;
using Microsoft.Owin.Security;
using System.ComponentModel;
using System;
using System.Text.RegularExpressions;


namespace SalesPortal.Web.Models
{
    public class GameHelper
    {
        private static Random rnd = new Random();
        public static readonly Regex DiceNotationRegEx = new Regex(@"^(?<num>\d+)?[dD]?(?<side>\d+)?$"); // Regex for dice notation

        public static int Dice(string diceNotation)
        {
            Match m = DiceNotationRegEx.Match(diceNotation);

            if (!m.Success)
            {
                return 0;
            }

            int number_of_dice = 1; // Defaults
            int number_of_side = 6;

            if (!int.TryParse(m.Groups["num"].Value, out number_of_dice))
            {
                number_of_dice = 1;
            }

            if (!int.TryParse(m.Groups["side"].Value, out number_of_side))
            {
                number_of_side = 6;
            }

            return Dice(number_of_side, number_of_dice);
        }

        public static int Dice(int side)
        {
            int current_number = rnd.Next(side);

            if (current_number == 0)
                return side;
            else
                return current_number;
        }

        public static int Dice(int side, int num)
        {
            int result = 0;

            for (int i = 0; i < num; i++)
            {
                int current_number = rnd.Next(side);

                if (current_number == 0)
                    result = result + side;
                else
                    result = result + current_number;
            }

            return result;
        }
    }
}